package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.constants.TableNameConstants;
import com.credithc.customers.dataservice.mapper.HYD.HydMapper;
import com.credithc.customers.dataservice.po.CustomerNoPo;
import com.credithc.customers.dataservice.po.SystemLendPo;
import com.credithc.customers.dataservice.po.SystemUserPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.service.OldHydDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OldHydDataServiceImpl implements OldHydDataService {
    @Autowired
    HydMapper hydMapper;
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Override
    public void oldOrder(Integer start) {
        boolean f =true;
        Long num = null;
        if (start == null) {
            num = hydMapper.queryLendMaxId();
        }else{
            num = Long.valueOf(start);
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    List<SystemLendPo> systemLendPos = hydMapper.queryHydLend(Long.toString(vernier.get()));
                    log.info("开始导入 从"+vernier.get() +"后10000条" );
                    if (systemLendPos.size() > 0) {
                        bulidHydLend(systemLendPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }


    }

    @Override
    public void oldBank(Integer start) {
        boolean f =true;
        Long num = null;
        if (start == null) {
            num = hydMapper.queryLendMaxId();
        }else{
            num = Long.valueOf(start);
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    List<SystemUserPo> systemLendPos = hydMapper.queryhydBank(Long.toString(vernier.get()));
                    log.info("开始导入 从"+vernier.get() +"后10000条" );
                    if (systemLendPos.size() > 0) {
                        bulidHydBank(systemLendPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }

    }

    private void bulidHydBank(List<SystemUserPo> systemLendPos) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (SystemUserPo u:systemLendPos) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("HYD");
            customerAccountInfoRo.setSystemSign("HYD");
            customerAccountInfoRo.setAccountCreateTime(u.getCreatTime());
            customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            customerAccountInfoRo.setAccountNo(u.getCustomerId());
            customerAccountInfoRo.setCustomerNo(u.getCustomerNo());
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setUserSourceId(u.getUserId());
            customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getUserSourceId()) && StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())) {
                customerAccountInfoRos.add(customerAccountInfoRo);
            }

        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }



    private void bulidHydLend(List<SystemLendPo> dlrLendRos) {
        List<CustomerProductInfoRo> customerProductInfoRos = new ArrayList<>();
        for (SystemLendPo d:dlrLendRos){
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName(TableNameConstants.HYD_DATABASE);
            customerProductInfoRo.setTableName(TableNameConstants.HYD_CUSTOMER_PRODUCT_JSD_LOAN_ORDER);
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setSubSystemSign("HYD");
            customerProductInfoRo.setSystemSign("HYD");
            customerProductInfoRo.setTableKey(d.getIncrementId() +"");
            customerProductInfoRo.setProductSourceSystem(CustomerConstants.HYR_CODE+"");
            customerProductInfoRo.setProductAmount(d.getAmount());
            customerProductInfoRo.setProductNo(d.getProductId());
            customerProductInfoRo.setProductStartTime(d.getCapitalEffectDate());
            customerProductInfoRo.setProductEndTime(d.getExpireDate());
            customerProductInfoRo.setProductDeadline(d.getPeriod()*30+"");
            customerProductInfoRo.setProductStatus(CustomerProductInfoServiceImpl.getYZTStatus("HYD"+d.getSts()));
            customerProductInfoRo.setUserResourceId(d.getUserId());
            CustomerNoPo byUserId = hydMapper.getByUserId(d.getUserId());
            if (byUserId !=null){
                customerProductInfoRo.setCustomerNo(byUserId.getCustomerNo());
                customerProductInfoRo.setPreCustomerNo(byUserId.getPreCustomerNo());
            }
            if (StringUtils.isNotBlank(customerProductInfoRo.getProductNo()) && StringUtils.isNotBlank(customerProductInfoRo.getUserResourceId()) && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {

                customerProductInfoRos.add(customerProductInfoRo);
            }
        }
        syncDataService.syncCustomerProduct(customerProductInfoRos);
    }

/*    private String getCustomerRole(String customerNo,String systemSign, String subSystemSign){
        String roleSource = commonService.getRoleSource(systemSign, subSystemSign);
        String destRole = null;
        if(StringUtils.isNotBlank(roleSource)){
            if(!CustomerConstants.RELATION_SOURCE_LIST.contains(roleSource)){
                //表示角色是固定值
                destRole =  roleSource;
            } else if (CustomerConstants.ROLE_SOURCE_PRODUCT.equals(roleSource)){

                destRole =  null;
            }

            if(StringUtils.isNotBlank(destRole)){
                CustomerRolePo customerRole = commonService.getCustomerRole(customerNo, destRole, systemSign, subSystemSign);
                if(customerRole == null){
                    return destRole;
                }
            }
        }
        return null;
    }*/
}
